package models;

import java.util.*;

import javax.persistence.*;

import play.db.ebean.*;
import play.db.ebean.Model.Finder;
import play.data.format.*;
import play.data.validation.*;
import com.avaje.ebean.*;

@Entity
public class UserQcm extends Model {

	@Id
	private Long idUserQcm;
	private User user;
	private Qcm qcm;
	private double note;
	private Date date;
	private int duree;

	private List<Choix> listeChoixUser = new ArrayList<Choix>();

	public static Finder<Long, UserQcm> find = new Finder<Long, UserQcm>(
			Long.class, UserQcm.class);

	/**
	 * La méthode permet de retrouver une question à partir de son ID
	 * 
	 * @param id
	 * @return Qcm
	 */
	public static UserQcm findById(Long id) {
		return Ebean.find(UserQcm.class, id);
	}

	public static UserQcm findByIds(Long idQcm, Long idUser) {
		return find.where()
				.eq("qcm", Qcm.findById(idQcm))
				.eq("user", User.findById(idUser))
				.findUnique();
	}
	
	public static List<UserQcm> findByStatut(Statut statut){
		return find.where().eq("statut", statut.name()).findList(); 
	}

	public static UserQcm authenticate(String log, String pwd) {
		return find.where().eq("login", log).eq("password", pwd).findUnique();
	}

	public static List<UserQcm> findAll() {
		return find.all();
	}

	public static void create(UserQcm admin) {
		admin.save();
	}

	public static void update(UserQcm admin) {
		admin.update();
	}

	public static void delete(Long id) {
		find.ref(id).delete();
	}
	@Override
	public String toString() {
		StringBuffer appdata = new StringBuffer(75);
		appdata.append("liste des choix :");
		for (Choix choix : listeChoixUser) {
			appdata.append("\n" + choix.getDescriptifChoix() + "\n");
		}
		return appdata.toString();
	}
	
	/*
	 * GETTER ET SETTER
	 */
	@ManyToOne
	public User getUser() {
		return user;
	}

	@ManyToOne
	public Qcm getQcm() {
		return qcm;
	}

	public double getNote() {
		return note;
	}

	public Date getDate() {
		return date;
	}

	public int getDuree() {
		return duree;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public void setQcm(Qcm qcm) {
		this.qcm = qcm;
	}

	public void setNote(double note) {
		this.note = note;
	}

	public void setDate(Date date) {
		this.date = date;
	}

	public void setDuree(int duree) {
		this.duree = duree;
	}

	public Long getIdUserQcm() {
		return idUserQcm;
	}

	public void setIdUserQcm(Long idUserQcm) {
		this.idUserQcm = idUserQcm;
	}

	@ManyToMany
	public List<Choix> getListeChoixUser() {
		return listeChoixUser;
	}

	public void setListeChoixUser(ArrayList<Choix> listeChoixUser) {
		this.listeChoixUser = listeChoixUser;
	}

}
